<?php
class ecTemplateHelpers {

  /**
   * Returns a <script> include tag per source given as argument.
   *
   * <b>Examples:</b>
   * <code>
   *  echo javascript_include_tag('xmlhr');
   *    => <script language="JavaScript" type="text/javascript" src="/js/xmlhr.js"></script>
   *  echo javascript_include_tag('common.javascript', '/elsewhere/cools');
   *    => <script language="JavaScript" type="text/javascript" src="/js/common.javascript"></script>
   *       <script language="JavaScript" type="text/javascript" src="/elsewhere/cools.js"></script>
   * </code>
   *
   * @param string asset names
   * @param array additional HTML compliant <link> tag parameters
   *
   * @return string XHTML compliant <script> tag(s)
   * @see    javascript_path
   */
  public static function javascript_include_tag()
  {
    $sources = func_get_args();
    $sourceOptions = (func_num_args() > 1 && is_array($sources[func_num_args() - 1])) ? array_pop($sources) : array();

    $html = '';
    foreach ($sources as $source)
    {
      $condition = null;
      if (isset($sourceOptions['condition']))
      {
        $condition = $sourceOptions['condition'];
        unset($sourceOptions['condition']);
      }

      $options = array_merge(array('type' => 'text/javascript', 'src' => $source), $sourceOptions);
      $tag = ecTemplateTagHelper::content_tag('script', '', $options);

      if (null !== $condition)
      {
        $tag = ecTemplateTagHelper::comment_as_conditional($condition, $tag);
      }

      $html .= $tag."\n";
    }

    return $html;
  }

  /**
   * Returns a css <link> tag per source given as argument,
   * to be included in the <head> section of a HTML document.
   *
   * <b>Options:</b>
   * - rel - defaults to 'stylesheet'
   * - type - defaults to 'text/css'
   * - media - defaults to 'screen'
   *
   * <b>Examples:</b>
   * <code>
   *  echo stylesheet_tag('style');
   *    => <link href="/stylesheets/style.css" media="screen" rel="stylesheet" type="text/css" />
   *  echo stylesheet_tag('style', array('media' => 'all'));
   *    => <link href="/stylesheets/style.css" media="all" rel="stylesheet" type="text/css" />
   *  echo stylesheet_tag('random.styles', '/css/stylish');
   *    => <link href="/stylesheets/random.styles" media="screen" rel="stylesheet" type="text/css" />
   *       <link href="/css/stylish.css" media="screen" rel="stylesheet" type="text/css" />
   * </code>
   *
   * @param string asset names
   * @param array  additional HTML compliant <link> tag parameters
   *
   * @return string XHTML compliant <link> tag(s)
   * @see    stylesheet_path
   */
  public static function stylesheet_tag()
  {
    $sources = func_get_args();
    $sourceOptions = (func_num_args() > 1 && is_array($sources[func_num_args() - 1])) ? array_pop($sources) : array();

    $html = '';
    foreach ($sources as $source)
    {
      $condition = null;
      if (isset($sourceOptions['condition']))
      {
        $condition = $sourceOptions['condition'];
        unset($sourceOptions['condition']);
      }

      $options = array_merge(array('rel' => 'stylesheet', 'type' => 'text/css', 'media' => 'screen', 'href' => $source), $sourceOptions);
      $tag = ecTemplateTagHelper::tag('link', $options);

      if (null !== $condition)
      {
        $tag = ecTemplateTagHelper::comment_as_conditional($condition, $tag);
      }

      $html .= $tag."\n";
    }

    return $html;
  }

   /**
   * Returns an <img> image tag for the asset given as argument.
   *
   * <b>Options:</b>
   * - 'alt'  - defaults to the file name part of the asset (capitalized and without the extension)
   * - 'size' - Supplied as "XxY", so "30x45" becomes width="30" and height="45"
   *
   * <b>Examples:</b>
   * <code>
   *  echo image_tag('foobar');
   *    => <img src="images/foobar.png" alt="Foobar" />
   *  echo image_tag('/my_images/image.gif', array('alt' => 'Alternative text', 'size' => '100x200'));
   *    => <img src="/my_images/image.gif" alt="Alternative text" width="100" height="200" />
   * </code>
   *
   * @param string $source  image asset name
   * @param array  $options additional HTML compliant <img> tag parameters
   *
   * @return string XHTML compliant <img> tag
   * @see    image_path
   */
  public static function image_tag($source, $options = array())
  {
    if (!$source)
    {
      return '';
    }

    $options = ecTemplateTagHelper::_parse_attributes($options);

    $options['src'] = $source;

    if (isset($options['alt_title']))
    {
      // set as alt and title but do not overwrite explicitly set
      if (!isset($options['alt']))
      {
        $options['alt'] = $options['alt_title'];
      }
      if (!isset($options['title']))
      {
        $options['title'] = $options['alt_title'];
      }
      unset($options['alt_title']);
    }

    if (!isset($options['alt']))
    {
      $path_pos = strrpos($source, '/');
      $dot_pos = strrpos($source, '.');
      $begin = $path_pos ? $path_pos + 1 : 0;
      $nb_str = ($dot_pos ? $dot_pos : strlen($source)) - $begin;
      $options['alt'] = ucfirst(substr($source, $begin, $nb_str));
    }

    if (isset($options['size']))
    {
      list($options['width'], $options['height']) = explode('x', $options['size'], 2);
      unset($options['size']);
    }

    return ecTemplateTagHelper::tag('img', $options);
  }
}


